<?php if(!defined('CALL')) exit('No direct script access allowed');

include_once('ctrl/page.class.php');
include_once('view/v_usercard.class.php');
include_once('view/v_menus.class.php');
include_once('view/v_event.class.php');
include_once('models/m_user.class.php');
include_once('models/m_events.class.php');
include_once('models/m_event_links.class.php');

class PageEvent extends Page
{
	private $event_id = 0;
	private $event = null;
	
	public function __construct($actor, $event_id)
	{
		$this->event_id = $event_id;
		// куда ведет ссылка с аватара актора (в правом верхнем углу)
		$this->page_name = "plans/own";
		parent::__construct($actor, $this->user);
		$this->prepare();
	}
	
	private function prepare()
	{
		$event = mEvents::get2($this->event_id, $this->actor);
		if($event != null)
		{
			$events = array($event);
			$events = mEvents::add_details($events, $this->actor->id);
			$this->event = $events[0];
			
			$this->user = new mUser($this->event["user_id"]);
			$this->user->load();
			$this->user->stat = mUser::get_stat($this->user->id);
		}
	}
	
	protected function gen_main()
	{
		$xtpl = vEvent::template_open();
		if(is_null($this->actor->id))
		{
			$text = 
				'Для анонимных посетителей этот раздел недоступен.<br/>'.
				'Вы должны авторизоваться (ввести свой email и пароль) '.
				'или <a href="'.ROOT_URL.'/signup/">зарегистрироваться</a>.';
				
			$html = vEvent::build_textInfo($text);	
		}
		elseif(is_null($this->event))
		{
			$text =
				'Указанного события не существует или'.
				' у вас нет прав доступа к нему';
				
			$html = vEvent::build_textInfo($text);	
		}
		elseif($_SESSION['categ'] == 'comments')
		{
			$comments = mEventLinks::select($this->event_id, 'COM');
			
			$html = vEvent::build_eventBox($this->event, $this->actor, $xtpl);
			$html .= vEvent::build_commentLine($this->event, $comments, $this->actor, $xtpl);
			$html .= vEvent::build_commentForm($xtpl);
		}
		else
		{
			$links = mEventLinks::select($this->event['event_id'], 'SHA');

			$users = array();
			if(count($links) > 0)
			{
				foreach($links as $link)
					array_push($users, $link["user_id"]);
				$list = implode(",", $users);
				$users = mUser::get_props($list, TRUE);
			}
			
			$xtpl = vEvent::template_open();
			$html = vEvent::build_eventBox($this->event, $this->actor, $xtpl);
			$html .= vEvent::build_userLine($this->event, $users, $this->actor, $xtpl);
		}
		return $html;
	}

	protected function gen_side()
	{
		$html = '';

		$menus = new vMenus();
		$html .= $menus->gen_search_form();
		
		if($this->user->id)
		{
			// $menus = new Menus();
			// $html .= $menus->gen_plans_menu($this->actor, $this->user);
			
			$card = new vUsercard();
			$html .= $card->generate($this->user, $this->actor, TRUE);
		}
		return $html;
	}

	protected function add_css()
	{
		return "";
	}

	protected function add_css_files()
	{
		return "";
	}

	protected function add_js()
	{
		return "\n\tvar categ = '".$_SESSION["categ"]."';\n";
	}

	protected function add_js_files()
	{
		return '<script type="text/javascript" src="'.ROOT_URL.'/js/EventProcessor.js"></script>';
	}
}
